package com.crscd.ai.controller;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Map;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 9/30/25 2:31 PM
 * @modifiedBy:
 * @version: 1.0
 */
@RestController
public class PromptTemplateController {
  @Resource(name = "deepseek")
  private ChatModel deepseekChatModel;

  @Resource(name = "qwen")
  private ChatModel qwenChatModel;

  @Resource(name = "deepseekChatClient")
  private ChatClient deepseekChatClient;

  @Resource(name = "qwenChatClient")
  private ChatClient qwenChatClient;

  @Value("classpath:/template/prompt_template.txt")
  private org.springframework.core.io.Resource userTemplate;

  @GetMapping("/promptTemplate/chat")
  public Flux<String> chat(String topic, String outputFormat, String wordCount) {
    PromptTemplate promptTemplate =
        new PromptTemplate(
            """
         讲一个关于{topic}的故事，
         并以{output_format}格式输出，
         字数在{word_count}左右
       """);
    Prompt prompt =
        promptTemplate.create(
            Map.of(
                "topic", topic,
                "output_format", outputFormat,
                "word_count", wordCount));
    return deepseekChatClient.prompt(prompt).stream().content();
  }

  @GetMapping("/promptTemplate/chat2")
  public Flux<String> chat2(String topic, String outputFormat) {
    PromptTemplate promptTemplate = new PromptTemplate(userTemplate);
    Prompt prompt = promptTemplate.create(Map.of("topic", topic, "output_format", outputFormat));
    return deepseekChatClient.prompt(prompt).stream().content();
  }

  @GetMapping("/promptTemplate/chat3")
  public String chat3(String sysTopic, String userTopic) {
    // 1. SystemPromptTemplate
    SystemPromptTemplate systemPromptTemplate =
        new SystemPromptTemplate("你是{systemTopic}助手，只回答{systemTopic}方面的问题，且以html格式返回");
    Message sysMessage = systemPromptTemplate.createMessage(Map.of("systemTopic", sysTopic));
    // 2. PromptTemplate
    PromptTemplate userPromptTemplate = new PromptTemplate("解释一下{userTopic}");
    Message userMessage = userPromptTemplate.createMessage(Map.of("userTopic", userTopic));
    // 3. 组合多个message->Prompt
    Prompt prompt = new Prompt(List.of(sysMessage, userMessage));
    // 4. 调用LLM
    return deepseekChatClient.prompt(prompt).call().content();
  }

  @GetMapping("/promptTemplate/chat4")
  public Flux<String> chat4(String question) {
    return deepseekChatClient.prompt().system("你是一个Java编程助手，拒绝回答非技术问题").user(question).stream()
        .content();
  }
}
